<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<title>合作机构平台_价值链</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link rel="stylesheet" type="text/css" href="/public/admin/css/layout.global.css" />
		<link rel="stylesheet" type="text/css" href="/public/plug/FormCombox/FormCombox.css" />
		<link rel="stylesheet" type="text/css" href="/public/plug/IconFont/style.css" />
		<link rel="stylesheet" type="text/css" href="/public/plug/editormd/css/editormd.css" />
		<style type="text/css">
			body * {
				font-size: 14px;
			}
			
			.bgmodal {
				background-image: url(/public/webfe/images/bg.jpg);
				background-size: cover;
				position: fixed;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 1;
			}
			
			.indexmodal {
				width: 560px;
				height: 400px;
				position: relative;
				top: 50%;
				margin-top: -200px;
				z-index: 2;
			}
			
			.contmodal {
				width: 960px;
				position: relative;
				z-index: 2;
			}
			
			.layout-tab-bar {
				line-height: 40px;
				padding-top: 8px;
				background-color: #F2F2F2;
			}
			
			.layout-tab-bar .layout-tab-item {
				display: inline-block;
				padding: 0 8px;
				border-radius: 4px 4px 0 0;
				margin-left: 16px;
				font-size: 14px;
			}
			
			.layout-tab-bar .layout-tab-item.active {
				background-color: #FFFFFF;
				border: 1px solid #E4E4E4;
				border-bottom-color: #FFFFFF;
			}
			
			.layout-tab-container {
				min-height: 586px;
			}
			
			.layout-tab-container .layout-tab-item {
				display: none;
			}
			
			.layout-tab-container .layout-tab-item.active {
				display: block;
			}
			
			.header {
				height: 80px;
				padding: 8px;
			}
			
			.nav-head a {
				display: inline-block;
				line-height: 40px;
				margin: 0 16px 0 0;
			}
			
			.nav-head a.active {
				color: #44B549;
				border-bottom: 1px solid #44B549;
			}
			
			.bg-eee {
				background-color: #EEEEEE;
			}
			
			.bg-fff {
				background-color: #FFFFFF;
			}
			
			.c-888 {
				color: #888888!important;
			}
			
			.lh40x {
				line-height: 40px;
			}
			
			table {
				text-align: center;
				line-height: 32px;
			}
			
			tbody td {
				line-height: 48px;
				border-bottom-width: 1px;
				border-bottom-style: solid;
				border-bottom-color: #ebebeb;
			}
			
			.loadmore-modal {
				display: block;
				width: 100%;
				height: 40px;
				line-height: 40px;
				text-align: center;
			}
			
			.btn-logout {
				/*padding: 4px 8px;*/
			}
			
			.overa {
				overflow: auto;
			}
			
			.position-relative {
				position: relative;
			}
			
			.box-buttom.tt {
				width: 120px;
				min-width: auto;
				position: absolute;
				top: 48px;
				right: 180px;
				z-index: 1;
			}
			
			.uploadFiles {
				width: 160px;
				height: 160px;
				position: relative;
			}
			
			.uploadFiles img {
				display: block;
				margin: 0 auto;
				max-width: 100%;
				max-height: 100%;
			}
			
			.uploadFiles input {
				display: block;
				width: 100%;
				height: 100%;
				border: none;
				outline: none;
				background-color: transparent;
				opacity: 0;
				position: absolute;
				top: 0;
				left: 0;
				cursor: pointer;
			}
			
			.editormd-dialog-header {
				line-height: normal!important;
			}
		</style>
	</head>

	<body class="bg-eee">

		<div class="bgmodal"></div>

		<?php if( !\think\Session::has('org') ){ ?>
		<div class="layout-region indexmodal m-x-auto bg-fff box-shadow">
			<div class="fs32r text-center p-y-4x">
				价值链 · 合作机构平台
			</div>
			<div class="layout-form p-t-4x" style="padding: 32px 80px 0 40px;">
				<div class="layout-combox "><label class="layout-form-label">登录名</label>
					<div class="layout-form-ipt"><input type="text" name="username" value="ag001"></div>
				</div>
				<div class="layout-combox "><label class="layout-form-label">密码</label>
					<div class="layout-form-ipt"><input type="password" name="password" value="ag001"></div>
				</div>
			</div>
			<div class="box-buttom clearfix p-b-4x" style="width: 240px; padding-left: 140px;">
				<a href="javascript:;" class="submit-buttom float-left" id="btn-login-submit">确定</a>
			</div>
		</div>
		<?php }else{ ?>
		<div class="layout-region contmodal m-x-auto overa">
			<div class="p-y-2x fs32r" style="color: #FFFFFF; font-weight: lighter;">
				价值链 · 合作机构平台
			</div>
			<div class="header bg-fff box-shadow m-y-2x clearfix">
				<img src="javascript:void(0);" id="bind-org-logo" class="float-left" height="100%" />
				<span class="float-left m-l-2x lh40x">
					<p id="bind-org-title" class="fs16r"></p>
					<p id="bind-org-url" class="c-888"></p>
				</span>
				<span class="float-right p-t-4x" style="">
					<a href="javascript:;" class="btn-logout fs14r c-888">
						退出登录
						<i class="icon-jump"></i>
					</a>
				</span>
			</div>
			<div class="container bg-fff box-shadow">
				<div class="layout-tab-bar">
					<a href="javascript:;" class="layout-tab-item active" data-itor="#tab1">发放优惠码</a>
					<a href="javascript:;" class="layout-tab-item" data-itor="#tab2">发放历史记录</a>
					<a href="javascript:;" class="layout-tab-item" data-itor="#tab3">编辑合作机构信息</a>
					<a href="javascript:;" class="layout-tab-item" data-itor="#tab4">修改登录密码</a>
				</div>
				<div class="layout-tab-container bg-fff">
					<div id="tab1" class="layout-tab-item active position-relative">
						<div class="layout-form" style="padding: 48px 320px 0 80px;">
							<div class="layout-combox "><label class="layout-form-label">手机号</label>
								<div class="layout-form-ipt"><input type="text" name="mobile"></div>
							</div>
						</div>
						<div class="box-buttom clearfix tt">
							<a href="javascript:;" class="submit-buttom float-left" id="btn-sendcode-submit">发送</a>
						</div>
					</div>
					<div id="tab2" class="layout-tab-item p-x-4x">
						<div class="nav-head p-y-3x m-b-3x b-line-b clearfix">
							<a href="javascript:;" class="active" layout-mark="list-model" data-model="0">全部</a>
							<a href="javascript:;" layout-mark="list-model" data-model="1">未使用</a>
							<a href="javascript:;" layout-mark="list-model" data-model="2">已使用</a>
						</div>
						<table width="100%" id="org-volist">
							<tr style="background-color: #e8e4e8;">
								<th>手机号</th>
								<th>优惠码</th>
								<th>发送时间</th>
							</tr>
							<tbody id="org-volist-view">

							</tbody>
						</table>
						<div class="loadmore-modal">
							<a href="javascript:;">点击加载更多</a>
						</div>
					</div>
					<div id="tab3" class="layout-tab-item">
						<div class="layout-form" id="layout-form-agency" style="padding: 48px 80px 0 80px;">
							<div class="layout-combox "><label class="layout-form-label">机构名称</label>
								<div class="layout-form-ipt"><input type="text" name="title"></div>
							</div>
							<div class="layout-combox uploadFilesBox"><label class="layout-form-label">机构LOGO</label>
								<div class="layout-form-ipt">
									<input type="hidden" name="logo" id="ipt-pc-logo" value="">
									<form class="uploadFiles" action="/*" onsubmit="return false;">
										<img src="/public/admin/images/upload-tips.png" id="bind-logo">
										<input layout-mark="uploadFile" type="file" multiple="" data-u="__ulogo">
									</form>
									<p class="c-888 fs14r m-t-1x">logo尺寸：160px * 160px</p>
								</div>
							</div>
							<div class="layout-combox "><label class="layout-form-label">机构网址</label>
								<div class="layout-form-ipt"><input type="text" name="url" value="javascript:;"></div>
							</div>
							<div class="layout-combox "><label class="layout-form-label">机构简介</label>
								<div class="layout-form-ipt">
									<div class="" id="content-editormd">
										<textarea id="content" name="introduction" style="height: 320px"></textarea>
									</div>
								</div>
							</div>
						</div>
						<div class="box-buttom p-b-6x clearfix" style="width: 240px; padding-left: 180px;">
							<a href="javascript:;" class="submit-buttom float-left" id="btn-agency-submit">提交</a>
						</div>
					</div>
					<div id="tab4" class="layout-tab-item">
						<form class="layout-form p-t-6x" id="layout-form-editpwd" action="/*" onsubmit="return false;" style="padding-left: 190px; padding-right: 300px;">
							<div class="layout-combox "><label class="layout-form-label">原密码</label>
								<div class="layout-form-ipt"><input type="password" name="passport" placeholder="请输入原密码"></div>
							</div>
							<div class="layout-combox "><label class="layout-form-label">新密码</label>
								<div class="layout-form-ipt"><input type="password" name="password" placeholder="请输入新密码"></div>
							</div>
							<div class="layout-combox "><label class="layout-form-label">再次输入</label>
								<div class="layout-form-ipt"><input type="password" name="confpwd" placeholder="再次输入新密码"></div>
							</div>
						</form>
						<div class="box-buttom clearfix p-b-4x" style="width: 240px; padding-left: 290px;">
							<a href="javascript:;" class="submit-buttom float-left fs14r" id="btn-editpwd-submit">确定</a>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			window.onload = function() {
				getOrgInfo();
			}
		</script>
		<?php } ?>

		<script type="text/html" id="project-volist-template">
			{{# for ( var i in d ) { }} {{# var list = d[i]; }}
			<tr>
				<td>{{ list.mobile }}</td>
				<td>{{ list.code }}</td>
				<td>{{ tool.formatDate(list.create_time) }}</td>
			</tr>
			{{# } }}
		</script>
		<script type="text/javascript" src="/public/admin/lib/jquery.1.12.3.min.js"></script>
		<script type="text/javascript" src="/public/plug/editormd/src/editormd.js"></script>
		<script type="text/javascript" src="/public/modules/upload.js"></script>
		<script type="text/javascript" src="/public/plug/layer/layer.min.js"></script>
		<script type="text/javascript" src="/public/plug/laytpl/laytpl.min.js"></script>
		<script type="text/javascript" src="/public/modules/tool.js"></script>
		<script type="text/javascript" src="/public/modules/interactive.js"></script>
		<script type="text/javascript">
			$(function() {
				$('.layout-tab-bar .layout-tab-item').on('click', function(e) {
					var e = e || window.event;
					e.stopPropagation();
					var that = $(this),
						itor = that.attr('data-itor');
					if(that.hasClass('active')) return;
					that.addClass('active').siblings('a').removeClass('active');
					$(itor).addClass('active').siblings('div').removeClass('active');
					checkTab(itor);
				});

				// 模式
				$('a[layout-mark=list-model]').on('click', function(e) {
					// 样式
					$(this).addClass('active').siblings('a').removeClass('active');
					// 操作
					$('#org-volist-view').html('');
					data_opt_volist['model'] = $(this).attr('data-model');
					data_opt_volist['page'] = 1;
					getOrgVolist();
				});

				// 加载更多
				$('.loadmore-modal a').on('click', function(e) {
					var next = $(this).data('next');
					if(next > 0) {
						data_opt_volist['page'] = next;
						getOrgVolist();
					}
				});

				// 绑定登录按钮
				$('#btn-login-submit').on('click', submitLogin);

				// 绑定登出按钮
				$('.btn-logout').on('click', submitLogout);

				// 发送验证码
				$('#btn-sendcode-submit').on('click', submitSendCode);

				// 上传图片
				$('[layout-mark=uploadFile]').on('change', function(e) {
					var picture = uploadFiles.getPicture(e);
					if(picture) {
						var u = $(this).attr('data-u');
						window[u].get($(this), picture);
						var fileInput = this.files[0];
						var form = new FormData();
						form.append("file", fileInput);
						form.append("name", "file");
						form.append("dir", 'upload');
						uploadFiles.getSrc('/api?common.upload', form, window[u]);
						this.parentNode.reset();
					}
				});

				// 绑定提交信息按钮
				$('#btn-agency-submit').on('click', submitAgency);

				// 绑定修改密码按钮
				$('#btn-editpwd-submit').on('click', submitEditpwd);
			});

			var template = document.getElementById('project-volist-template').innerHTML,
				data_opt_volist = dataOptVolist(),
				jude_volist = false,
				jude_editorg = false;

			// 上传图片回调
			var __ulogo = {
				'get': function(obj, src) {
					obj.parent().children('img').attr('src', src);
				},
				'onload': function(res) {
					if(res.status == 201) {
						$('#ipt-pc-logo').val(res.data.uri);
					} else {
						window.parent.system.error(res.error);
					}
				},
				'onerror': function(res) {
					window.parent.system.error('上传失败，请检测网络');
				}
			};

			// 切换标签回调
			function checkTab(itor) {
				if(itor == '#tab2') {
					if(!jude_volist) {
						getOrgVolist();
						jude_volist = true;
					}
				}
				if(itor == '#tab3') {
					if(!jude_editorg) {
						initEditor();
						jude_editorg = true;
					}
				}
			}

			// 修改密码
			function submitEditpwd() {
				var data_opt = {};
				$('#layout-form-editpwd').find('input[type=password]').each(function() {
					var name = this.name;
					data_opt[name] = this.value;
				});
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().put('/api?org.updatePwd', data_opt, function(res) {
					layer.closeAll('loading');
					if(res.status == 201) {
						layer.msg('修改成功', {
							icon: 1,
							time: 1200
						}, function() {
							//submitLogout();
						});
					} else {
						layer.msg(res.error, {
							icon: 2,
							time: 800
						});
					}
				});
			}

			// 修改合作机构
			function submitAgency() {
				var data_opt = {};
				$('#layout-form-agency').find('input[type=hidden],input[type=text]').each(function() {
					var name = this.name;
					data_opt[name] = this.value;
				});
				// 转码
				var str = $('#content').val();
				str = str.replace(/</gi, "&lt;");
				data_opt['introduction'] = str;
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().put('/api?org.complete', data_opt, function(res) {
					layer.closeAll('loading');
					if(res.status == 201) {
						layer.msg('修改成功', {
							icon: 1,
							time: 1200
						}, function() {
							window.location.reload();
						});
					} else {
						layer.msg(res.error, {
							icon: 2,
							time: 800
						});
					}
				});
			}

			// 获取合作机构信息
			function getOrgInfo() {
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().get('/api?org.get', function(res) {
					layer.closeAll('loading');
					if(res.status == 200) {
						var list = res.data;
						$('#bind-org-logo').attr('src', list.logo);
						$('#bind-org-title').text(list.title);
						$('#bind-org-url').html((list.url == 'javascript:;' ? '<em>未设置<em>' : '<a href="' + list.url + '" target="_blank">' + list.url + '</a>'));
						$('input[name=title]').val(list.title);
						$('input[name=logo]').val(list.logo);
						$('#bind-logo').attr('src', list.logo);
						$('input[name=url]').val(list.url);
						var cont = list.introduction;
						cont = cont.replace(/&lt;/gi, '<');
						$('textarea[name=introduction]').val(cont);
						
					}
				});
			}

			// 初始化编辑器
			function initEditor() {
				var editor = editormd("content-editormd", {
					width: '100%',
					height: 540,
					path: '/public/plug/editormd/lib/',
					codeFold: true,
					saveHTMLToTextarea: true,
					searchReplace: true,
					htmlDecode: "style,script,iframe|on*",
					emoji: true,
					taskList: true,
					toolbarIcons: ["undo", "redo", "image", "code", "preformatted-text", "code-block", "table", "|", "watch", "preview", "fullscreen"],
					tocm: true, // Using [TOCM]
					tex: true, // 开启科学公式TeX语言支持，默认关闭
					flowChart: true, // 开启流程图支持，默认关闭
					sequenceDiagram: true, // 开启时序/序列图支持，默认关闭,
					imageUpload: true,
					imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
					imageUploadURL: "/api?common.editUpload",
					onload: function() {
						//console.log('onload', this);
					}
				});
			}

			// 登录
			function submitLogin() {
				var data_opt = {};
				$('.indexmodal').find('input').each(function() {
					var name = this.name;
					data_opt[name] = this.value;
				});
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().post('/api?org.login', data_opt, function(res) {
					layer.closeAll('loading');
					if(res.status == 201) {
						layer.msg('登录成功', {
							icon: 1,
							time: 1200
						}, function() {
							window.location.reload();
						});
					} else {
						layer.msg(res.error, {
							icon: 2,
							time: 800
						});
					}
				});
			}

			// 登出
			function submitLogout() {
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().del('/api?org.logout', function(res) {
					layer.closeAll('loading');
					if(res.status == 204) {
						window.location.reload();
					} else {
						layer.msg(res.error, {
							icon: 2,
							time: 800
						});
					}
				});
			}

			// 发送优惠码
			function submitSendCode() {
				var data_opt = {
					'mobile': $('input[name=mobile]').val()
				};
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().post('/api?org.sendCode', data_opt, function(res) {
					layer.closeAll('loading');
					if(res.status == 201) {
						layer.msg('发送成功', {
							icon: 1,
							time: 1200
						}, function() {
						});
					} else {
						layer.msg(res.error, {
							icon: 2,
							time: 800
						});
					}
				});
			}

			// 加载列表
			function getOrgVolist(model) {
				layer.load(0, {
					shade: [0.08, '#000']
				});
				ajax().get('/api?org.volist', data_opt_volist, function(res) {
					layer.closeAll('loading');
					if(res.status >= 200 && res.status < 300) {
						$('.loadmore-modal a').data('next', res.data.next);
						if(res.data.list.length > 0) {
							// 渲染列表
							var tpl = laytpl(template);
							$('#org-volist-view').append(tpl.render(res.data.list));
							// 判断是否有分页
							if(res.data.next > res.data.page) {
								$('.loadmore-modal a').html('点击加载更多');
							} else {
								$('.loadmore-modal a').html('');
							}
						} else {
							$('.loadmore-modal a').html('暂无记录');
						}
					} else {
						window.parent.system.error(res.error);
					}
				});
			}

			function dataOptVolist(opts) {
				return tool.optionExtend({
					'page': 1, // 当前页数
					'show': 10, // 显示条数
					'model': 0 // 模式,0-全部(默认) 1-未使用 2-已使用
				}, opts || {});
			}
		</script>

	</body>

</html>